package com.chen.pspms.controller;

import com.chen.pspms.entity.Holder;
import com.chen.pspms.service.HolderSercice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/holder")
public class HolderController {
    @Autowired
    HolderSercice holderSercice;

    @RequestMapping("/list")
    public String findAll(Model model){
        List<Holder> holderlist = holderSercice.findyAll();
        model.addAttribute("holderlist",holderlist);
        return "持证人管理/持证人管理页面";
    }

    @RequestMapping("/delete")
    public String delete(Integer id) {
        holderSercice.delete(id);
        return "redirect:/holder/list";
    }

    @RequestMapping("/show")
    public String show(Integer id, Model model){
        //调用业务处理
        if(id==0) {
            //如果是新增，直接返回新增页面
            return "持证人管理/新建";
        }else {
            //如果是修改，查询数据后返回
            Holder holder = holderSercice.findyById(id);
            model.addAttribute("holder", holder);
        }
        //处理业务返回结果
        return "持证人管理/修改";
    }

    @RequestMapping("/save")
    public String save(Holder holder, Model model) {
        //调用业务处理
        String exist =null;
        if(holderSercice.findyByName(holder.getName())!=null) {
            //如果员工合同表已存在
            exist="该持证人已存在";
        }else {
            //合同不存在，则添加
            System.out.println(holder);
            holderSercice.save(holder);
        }
        //处理业务返回结果
        List<Holder> holderlist = holderSercice.findyAll();
        model.addAttribute("holderlist", holderlist);
        model.addAttribute("exist", exist);
        return "持证人管理/持证人管理页面";
    }

    @RequestMapping("/update")
    public String update(Holder holder, Model model) {
        //调用业务处理
        holderSercice.update(holder);
        //处理业务返回结果
        return "redirect:/holder/list";//重定向就是在字符串前面加上前�? redirect:
    }
}
